package com.zyx.controller;

/**
 * @author zyx
 * @date 2022/1/8
 */

import com.zyx.pojo.Admin;
import com.zyx.pojo.Spencer;
import com.zyx.pojo.User;
import com.zyx.service.AdminService;
import com.zyx.service.SpencerService;
import com.zyx.service.UserService;
import com.zyx.vo.GetSession;
import com.zyx.vo.ResultVo;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.HttpCookie;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("Login")
@ResponseBody
public class LoginController {
    @Resource
    private AdminService adminService;
    @Resource
    private SpencerService spencerService;
    @Resource
    private UserService userService;


    @InitBinder
    protected void initDateFormatBinder(WebDataBinder dataBinder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    //管理员登录
    @ResponseBody
    @RequestMapping(value = "user", method = RequestMethod.POST)
    public ResultVo<Admin> login(Admin admin, HttpServletRequest req, HttpServletResponse resp) {
        List<Admin> login = adminService.Login(admin);
        //登录成功把数据存入cookie
        if (login.size() > 0) {
            Admin userAdmin = login.get(0);
            HttpSession session = req.getSession();
            req.getSession().setAttribute("admin" + userAdmin.getUser(), userAdmin);
            Cookie cookie = new Cookie("admin", userAdmin.getUser());
            cookie.setMaxAge(60 * 30);
            cookie.setPath("/");
            resp.addCookie(cookie);
            return new ResultVo<>();
        } else {
            return new ResultVo<>(500, "登录失败");
        }
    }

    //店铺登录
    @ResponseBody
    @RequestMapping(value = "spencer", method = RequestMethod.POST)
    public ResultVo<Spencer> loginSpencer(Spencer spencer, HttpServletRequest req, HttpServletResponse resp) {
        System.out.println(spencer.getSpencerUser());
        List<Spencer> login = spencerService.Login(spencer);
        if (login.size() > 0) {
            Spencer s = login.get(0);
            HttpSession session = req.getSession();
            req.getSession().setAttribute("spencer" + s.getSpencerName(), s);
            Cookie cookie = new Cookie("spencer", s.getSpencerName());
            cookie.setMaxAge(60 * 30);
            cookie.setPath("/");
            resp.addCookie(cookie);
            return new ResultVo<>(s);
        } else {
            return new ResultVo<>(500, "登录失败");
        }
    }

    //管理员退出登录
    @ResponseBody
    @RequestMapping(value = "Logout", method = RequestMethod.POST)
    public ResultVo loginOut(HttpServletRequest req, HttpServletResponse resp) {
        GetSession s = new GetSession();
        HttpSession session = req.getSession();
        Admin a = s.adminSession(req, resp, session);
        session.removeAttribute("admin" + a.getUser());
        return new ResultVo<>();
    }

    //店铺退出登录
    @ResponseBody
    @RequestMapping(value = "spencerLogout", method = RequestMethod.POST)
    public ResultVo loginOutSpencer(HttpServletRequest req, HttpServletResponse resp) {
        GetSession s = new GetSession();
        HttpSession session = req.getSession();
        Spencer a = s.spencerSession(req, resp, session);
        session.removeAttribute("spencer" + a.getSpencerName());
        return new ResultVo<>();
    }

    //用户登录
    @ResponseBody
    @RequestMapping(value = "userLogin", method = RequestMethod.POST)
    public ResultVo userLogin(User user, HttpServletRequest req, HttpServletResponse resp) {
        List<User> login = userService.login(user);
        if (login.size() > 0) {
            User u = login.get(0);
            if (u.getState()==3){
                return new ResultVo(500,"用户已被注销，请重新注册或联系管理员");
            }
            HttpSession session = req.getSession();
            req.getSession().setAttribute("user" + u.getPhone(), u);
            Cookie cookie = new Cookie("user", u.getPhone());
            cookie.setMaxAge(60 * 30);
            cookie.setPath("/");
            resp.addCookie(cookie);
            return new ResultVo<>();
        }
        return new ResultVo<>(500, "登录失败");
    }

    //用户退出登录
    @ResponseBody
    @RequestMapping(value = "userLogout", method = RequestMethod.POST)
    public ResultVo loginOutUser(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session = req.getSession();
        User a = new User();
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            if("user".equals(cookie.getName())){
                a = (User) session.getAttribute("user"+cookie.getValue());
            }
        }
        Cookie cookie = new Cookie("user","");
        cookie.setMaxAge(0);
        resp.addCookie(cookie);
        session.removeAttribute("user" + a.getPhone());
        return new ResultVo<>(200,"登出成功");
    }

    //判断用户是否登录
    @ResponseBody
    @RequestMapping(value = "LoginBy", method = RequestMethod.POST)
    public ResultVo<User> noLogin(HttpServletRequest req,HttpSession session) {
        User user = new User();
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            if("user".equals(cookie.getName())){
                user = (User) session.getAttribute("user"+cookie.getValue());
                if(user!=null){
                    return new ResultVo<>(user);
                }
            }
        }
        return new ResultVo<>(500, "没有登录");
    }
}
